package g;

import c.b.a.a.a;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.Deflater;
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

/* compiled from: DeflaterSink */
public final class i implements y {
    public final f a;
    public final Deflater b;
    public boolean c;

    public i(f fVar, Deflater deflater) {
        this.a = fVar;
        this.b = deflater;
    }

    public a0 B() {
        return this.a.B();
    }

    public void E(e eVar, long j) throws IOException {
        b0.b(eVar.b, 0, j);
        while (j > 0) {
            v vVar = eVar.a;
            int min = (int) Math.min(j, (long) (vVar.c - vVar.b));
            this.b.setInput(vVar.a, vVar.b, min);
            a(false);
            long j2 = (long) min;
            eVar.b -= j2;
            int i = vVar.b + min;
            vVar.b = i;
            if (i == vVar.c) {
                eVar.a = vVar.a();
                w.a(vVar);
            }
            j -= j2;
        }
    }

    @IgnoreJRERequirement
    public final void a(boolean z) throws IOException {
        v x0;
        e A = this.a.A();
        while (true) {
            int deflate;
            x0 = A.x0(1);
            Deflater deflater;
            byte[] bArr;
            int i;
            if (z) {
                deflater = this.b;
                bArr = x0.a;
                i = x0.c;
                deflate = deflater.deflate(bArr, i, 8192 - i, 2);
            } else {
                deflater = this.b;
                bArr = x0.a;
                i = x0.c;
                deflate = deflater.deflate(bArr, i, 8192 - i);
            }
            if (deflate > 0) {
                x0.c += deflate;
                A.b += (long) deflate;
                this.a.Z();
            } else if (this.b.needsInput()) {
                break;
            }
        }
        if (x0.b == x0.c) {
            A.a = x0.a();
            w.a(x0);
        }
    }

    public void close() throws IOException {
        if (!this.c) {
            Throwable th = null;
            try {
                this.b.finish();
                a(false);
            } catch (Throwable th2) {
                th = th2;
            }
            try {
                this.b.end();
            } catch (Throwable th3) {
                if (th == null) {
                    th = th3;
                }
            }
            try {
                this.a.close();
            } catch (Throwable th32) {
                if (th == null) {
                    th = th32;
                }
            }
            this.c = true;
            if (th != null) {
                Charset charset = b0.a;
                throw th;
            }
        }
    }

    public void flush() throws IOException {
        a(true);
        this.a.flush();
    }

    public String toString() {
        StringBuilder g = a.g("DeflaterSink(");
        g.append(this.a);
        g.append(")");
        return g.toString();
    }
}
